﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TangchiController : MonoBehaviour
{
    public GameObject tangchi;
    public GameObject tanhuo;
    public ParticleSystem zhengqi;
    public Wenduji wenduji;
    public Material xin;
    public bool isGameStart = false;
    public float currentWendu = 20;
    public float manyidu = 0;
    public float manyiduDelta = 0.004f;
    public bool jiaoshui = false;

    float tanhuoFloat = 1.65f;
    float tanhuoFloatController = 1.4f;
    float wenduDelta = 0;

    // Update is called once per frame
    void Update()
    {
        if (isGameStart)
        {
            if (Input.GetMouseButtonDown(0))
            {
                if (!jiaoshui)
                {
                    StopAllCoroutines();
                    StartCoroutine(Baise());
                    wenduDelta = 1.2f - currentWendu / 100.00f;
                    jiaoshui = true;
                }
                tangchi.GetComponent<Animation>().Play("water01");
            }
            if (Input.GetMouseButtonUp(0))
            {
                if (jiaoshui)
                {
                    StopAllCoroutines();
                    wenduDelta = -1.3f + (100 - currentWendu) / 80.00f;
                    jiaoshui = false;
                }
                tangchi.GetComponent<Animation>().Play("water02");
            }

            currentWendu = currentWendu + wenduDelta;
            if (currentWendu >= 100)  
                currentWendu = 100;
            if (currentWendu <= 20)
                currentWendu = 20;
            wenduji.SetPoint(currentWendu);

            if (currentWendu > StepController.instance.wendu - StepController.instance.range / 2 && currentWendu < StepController.instance.wendu + StepController.instance.range / 2)
            {
                StepController.instance.StartCountDown(0);
                manyidu += manyiduDelta;
            }
            else if (currentWendu <= StepController.instance.wendu - StepController.instance.range / 2)
            {
                StepController.instance.StartCountDown(-1);
                manyidu -= 0.01f;
            }
            else if (currentWendu >= StepController.instance.wendu + StepController.instance.range / 2)
            {
                StepController.instance.StartCountDown(1);
                manyidu -= 0.01f;
            }

            if (manyidu <= 0.05f)
                manyidu = 0.05f;
            if (manyidu >= 0.5f)
                manyidu = 0.5f;
            xin.SetFloat("_control", manyidu);
            xin.SetFloat("_color_control", manyidu * 2.2f);

            if (jiaoshui)
            {
                wenduDelta += -0.012f;
                if (wenduDelta <= 0.2f)
                    wenduDelta = 0.2f;
                if (tanhuoFloat >= 0.1f)
                    tanhuoFloat = tanhuoFloat - 0.08f;
            }
            else
            {
                wenduDelta += 0.012f;
                if (wenduDelta >= -0.2f)
                    wenduDelta = -0.2f;
                if (tanhuoFloat <= 1.4f)
                    tanhuoFloat = tanhuoFloat + 0.08f;
            }

            if (tanhuoFloat < 1.4f)
                tanhuoFloatController = tanhuoFloat;
            else
                tanhuoFloatController = 1.4f;

            if (!jiaoshui && tanhuoFloat > 1.4f)
                tanhuoFloat = 1.65f;

            if (tanhuoFloat > 0.2f && !jiaoshui)
            {
                tanhuo.GetComponent<Renderer>().material.SetFloat("_node_313", 0);
                zhengqi.Stop();
            }
            tanhuo.GetComponent<Renderer>().material.SetFloat("_tanhuo_intensity", tanhuoFloatController);
        }
    }

    IEnumerator Baise()
    {
        yield return new WaitForSeconds(0.06f);
        tanhuo.GetComponent<Renderer>().material.SetFloat("_node_313", 1);
        zhengqi.Play();
    }

    public void Reset()
    {
        isGameStart = false;
        zhengqi.Stop();
        manyidu = 0;
        currentWendu = 20;
        wenduji.Restart(currentWendu);
        jiaoshui = false;
        StopAllCoroutines();
    }
}